CDR.Constant.USER_TYPE = 'USER';
CDR.Constant.PBX_TYPE = 'PBX';
//create user'form
Ext.define('CDR.widget.UserForm', {
    extend: 'Ext.form.Panel',
    alias : 'widget.userform',
	buttonAlign:'center',
	border: false,
	title: CDR.I18N['user.form.title'],
	bodyStyle:'padding:5px 5px 0',
	targetObject: null,
	type: null,
	fieldDefaults: {
		msgTarget: 'side',
		labelWidth: 65
	},
	initData: function(data, tree){
		/*if(data) {
			if(data.result) {
				if(data.result.sites) {
					var assignTo = this.down('combo[name=assignTo]');
					//load data to combobox
					assignTo.store.loadData(data.result.sites);
					//get selected node from tree
					if(tree){
						var node = tree.getSelectedNode();
						if(node) {
							assignTo.setValue(node.getId());
						}
					}
				}
			}
		}*/
		this.type = CDR.Constant.WINDOW_NEW_MODE;
	},
	eidtData: function(data){
		if(data) {
			if(data.result) {
				/*if(data.result.sites) {
					var assignTo = this.down('combo[name=assignTo]');
					//load data to combobox
					assignTo.store.loadData(data.result.sites);
					if(data.result.assignTo){
						assignTo.setValue(data.result.assignTo);
					}
				}*/
				var name = this.down('textfield[name=name]');
				name.setValue(data.result.name);
				var station = this.down('textfield[name=station]');
				station.setValue(data.result.station);
				var authorizationCode = this.down('textfield[name=authorizationCode]');
				authorizationCode.setValue(data.result.authorizationCode);
				var fullName = this.down('textfield[name=fullName]');
				fullName.setValue(data.result.fullName);
				var birthday = this.down('datefield[name=birthday]');
				birthday.setValue(data.result.birthDay);
				var email = this.down('textfield[name=email]');
				email.setValue(data.result.email);
				var phone = this.down('textfield[name=mobile]');
				phone.setValue(data.result.mobile);
				var home = this.down('textfield[name=home]');
				home.setValue(data.result.home);
				var fax = this.down('textfield[name=fax]');
				fax.setValue(data.result.fax);
				var phone = this.down('textfield[name=mobile]');
				phone.setValue(data.result.mobile);
				var address = this.down('textareafield[name=address]');
				address.setValue(data.result.address);
				var gender = this.down('combo[name=gender]');
				gender.setValue(data.result.gender);
				var userId = this.down('hidden[name=userId]');
				userId.setValue(data.result.userId);
				var password = this.down('textfield[name=password]');
				password.setValue(data.result.password);
			}
			
		}
		this.type = CDR.Constant.WINDOW_EDIT_MODE;
	},
	items: [{
		xtype: 'container',
		anchor: '100%',
		layout:'column',
		items: [{
			xtype:'container',
			columnWidth:.25,
			layout: 'anchor',
			items: [{
				xtype:'hidden',
				name:'userId',
			},{
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.name'],
				vtype:'name',
				name:'name',
				allowBlank: false,
				anchor:'96%',
				labelWidth: 50,
				listeners:{
					render: function(){
						var me =  this.up('panel');
						Ext.createWidget('checknameValidator',{targetObject: this, checkOn: 'USER', listenerObject:{target: me, event:'added'}});
					}
				}
			}, {
				xtype:'datefield',
				fieldLabel: CDR.I18N['user.form.birthday'],
				name:'birthday',
				allowBlank: false,
				anchor:'96%',
				labelWidth: 50
			},{
				fieldLabel: CDR.I18N['user.form.gender'],
				anchor:'96%',
				labelWidth:50,
				xtype: 'combo',
				mode:  'local',
				value: 'MALE',
				triggerAction:  'all',
				forceSelection: true,
				editable: false,
				name: 'gender',
				displayField: 'name',
				valueField: 'value',
				queryMode: 'local',
				store: Ext.create('Ext.data.Store', {
					fields : ['name', 'value'],
					data   : [
						{name : 'Male',   value: 'MALE'},
						{name : 'FeMale',  value: 'FEMALE'}
					]
				})
			},{
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.station'],
				name:'station',
				allowBlank: false,
				anchor:'96%',
				labelWidth: 50
			},{
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.authorization-code'],
				name:'authorizationCode',
				anchor:'96%',
				labelWidth: 110
			}]
		}, {
			xtype:'container',
			columnWidth:.55,
			layout: 'anchor',
			items: [{
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.full-name'],
				name:'fullName',
				allowBlank: false,
				anchor:'96%',
				labelWidth: 75
			},{
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.email'],
				name:'email',
				allowBlank: false,
				anchor:'96%',
				labelWidth: 75,
				vtype: 'email'
			},{
				xtype:'textareafield',
				fieldLabel: CDR.I18N['user.form.address'],
				name:'address',
				allowBlank: false,
				anchor:'96%',
				labelWidth: 75
			}]
		}, {
			xtype:'container',
			columnWidth:.20,
			layout: 'anchor',
			items: [{
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.mobile'],
				name:'mobile',
				allowBlank: false,
				anchor:'98%',
				labelWidth:70,
				vtype:'number'
			}, {
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.home'],
				name:'home',
				anchor:'98%',
				labelWidth:70
			}, {
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.fax'],
				name:'fax',
				anchor:'98%',
				labelWidth:70
			}, {
				xtype:'textfield',
				fieldLabel: CDR.I18N['user.form.password'],
				name:'password',
				inputType:'password', 
				allowBlank: false,
				anchor:'98%',
				labelWidth:70
			}]
		}]
	}]
	,
	buttons:[
		{
			text: 'Save',
			iconCls: 'icon-save',
			formBind: true,
			handler: function() {
				var userForm = this.up('userform');
				var form = this.up('form').getForm();
				var urlSubmit = "";
				if (form.isValid()) {
					if(userForm.type === CDR.Constant.WINDOW_NEW_MODE){
						urlSubmit = "create-user";
					} else {
						urlSubmit = "edit-user";
					}
					form.submit({
						url: urlSubmit,
						success: function(form, action) {
						   Ext.Msg.alert('Success', action.result.msg);
						   form.reset();
						   if(userForm.targetObject) {
								userForm.targetObject.submitOk(action.result, userForm.type);
						   }
						},
						failure: function(form, action) {
							Ext.Msg.alert('Failed', action.result.msg);
						}
					});
				}
			}
		},{	
			text: 'Reset',
			handler: function () {
				this.up('form').getForm().reset();
			}
		}
	]
});



Ext.define('CDR.widget.UserAssign', {
    extend: 'Ext.form.Panel',
    alias : 'widget.userassign',
	buttonAlign:'center',
	border: false,
	title: CDR.I18N['organization.assign-user.form.title'],
	layout:'anchor',
	bodyPadding: 10,
	treeView: null,
	fieldDefaults: {
		msgTarget: 'side',
		labelWidth: 65
	},
	initComponent: function() {
		var me = this;
		me.callParent();
	   this.itemSelector = Ext.createWidget('itemselector', {
	        name: 'itemselector',
	        anchor: '100%',
	        store: Ext.create('Ext.data.Store', {
	            fields: ['obId', 'name']
	        }),
	        displayField: 'name',
	        valueField: 'obId',
	        border: false,
	        allowBlank: false,
	        msgTarget: 'side'
	    });
	   
	   this.assignTo = Ext.createWidget('combo', {
			fieldLabel:CDR.I18N['organization.assign-user.form.assign-to'],
			anchor:'20%',
			labelWidth:75,
			mode:  'local',
			triggerAction:  'all',
			forceSelection: true,
			editable: false,
			name: 'assignTo',
			displayField: 'name',
			allowBlank: false,
			valueField: 'obId',
			queryMode: 'local',
			store: Ext.create('Ext.data.Store', {
				fields : ['name', 'obId']
			})
		});
	   this.add(this.itemSelector);
	   this.add(this.assignTo);
	},
	editData: function(data, tree){
		if(data) {
			this.itemSelector.bindStore(Ext.create('Ext.data.Store', {
	            fields: ['obId', 'name'],
	            data: data.result.users
	        }));
			this.assignTo.store.loadData(data.result.sites);
			if(tree){
				var node = tree.getSelectedNode();
				if(node) {
					this.assignTo.setValue(node.getId());
				}
			}
		}
	},
	buttons:[
		{
			text: CDR.I18N['button.save'],
			iconCls: 'icon-save',
			handler: function() {
				var userForm = this.up('userassign');
				var form = this.up('form').getForm();
				if (form.isValid()) {
					form.submit({
						success: function(form, action) {
						   Ext.Msg.alert('Success', action.result.msg);
						   if(userForm.treeView){
							  	var nodeValue = userForm.assignTo.getValue(); 
							  	userForm.treeView.reloadSameItemClick(nodeValue);
						   }
						},
						failure: function(form, action) {
							Ext.Msg.alert('Failed', action.result.msg);
						}
					});
				}
			}
		},{	
			text: CDR.I18N['button.reset'],
			handler: function () {
				this.up('form').getForm().reset();
			}
		}
	]
});